#! /usr/bin/env python
# -*- coding: utf-8 -*-

from django.shortcuts import render, redirect
from utils.tencent.cos import delete_bucket
from web import models


def setting(request, project_id):

    return render(request, 'setting.html')


def setting_delete(request, project_id):
    if request.method == "GET":
        return render(request, 'setting_delete.html')

    if request.method == "POST":
        project_name = request.POST.get('project_name')
        if not project_name or project_name != request.tracer.project.name:
            return render(request, "setting_delete.html", {'err': '项目名错误'})

        # 删除项目(只有创建者可以删除)
        if request.tracer.user != request.tracer.project.creator:
            return render(request, "setting_delete.html", {'err': '当前用户无权限删除项目！'})

        # 1. 删除桶
        # 1.1 删除桶中的文件（找到桶中的文件 删除文件）
        # 1.2 删除所有碎片
        # 1.3 删除桶
        delete_bucket(request.tracer.project.bucket, request.tracer.project.region)

        # 2. 删除项目
        models.Project.objects.filter(id=request.tracer.project.id).delete()

        return redirect("project_list")


